package myJs

import myJs.Implicits._
import myJs.Utils.{dataRow, dataValue}
import myJs.myPkg._
import myJs.myPkg.jquery._
import org.scalajs.dom.ext.Ajax
import scalatags.Text.all._

import scala.collection.immutable.SeqMap
import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.JSON
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import js.Dynamic.{global => g}
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 6/5/2021
 */
@JSExportTopLevel("Statistics")
object Statistics {

  val formId = "form"
  val tableId = "table"
  val modalId = "modal"
  val updateTableId = "updateTable"
  val numberTableId = "numberTable"
  val sampleTableId = "sampleTable"

  @JSExport("init")
  def init = {
    registerEvent
    refreshTable

  }

  def refreshTable={
    refreshUpdateTable
    refreshSampleTable
    refreshNumberTable
  }

  def refreshUpdateTable = {
    val url = g.jsRoutes.controllers.ToolController.getFileData().url.toString
    Ajax.get(url = s"${url}?fileName=update.txt").map { xhr =>
      val rs = xhr.responseText.toJDJAn
      val columnNames = rs("columnNames").asInstanceOf[js.Array[String]]
      val columns = columnNames.map { columnName =>
        val fmt = columnName
        ColumnOptions.field(columnName).title(columnName).sortable(true)
      }
      val options = TableOptions.data(rs("array")).columns(columns).search(false).pagination(false)
      $(s"#${updateTableId}").bootstrapTable(options)
    }
  }

  def refreshNumberTable = {
    val url = g.jsRoutes.controllers.ToolController.getFileData().url.toString
    Ajax.get(url = s"${url}?fileName=number.txt").map { xhr =>
      val rs = xhr.responseText.toJDJAn
      val columnNames = rs("columnNames").asInstanceOf[js.Array[String]]
      val columns = columnNames.map { columnName =>
        val fmt = columnName
        ColumnOptions.field(columnName).title(columnName).sortable(false)
      }
      val options = TableOptions.data(rs("array")).columns(columns).search(false).pagination(false)
      $(s"#${numberTableId}").bootstrapTable(options)
    }
  }

  def refreshSampleTable = {
    val url = g.jsRoutes.controllers.ToolController.getFileData().url.toString
    Ajax.get(url = s"${url}?fileName=sample.txt").map { xhr =>
      val rs = xhr.responseText.toJDJAn
      val columnNames = rs("columnNames").asInstanceOf[js.Array[String]]
      val columns = columnNames.map { columnName =>
        val fmt = columnName
        ColumnOptions.field(columnName).title(columnName).sortable(false)
      }
      val options = TableOptions.data(rs("array")).columns(columns).search(false).pagination(false)
      $(s"#${sampleTableId}").bootstrapTable(options)
    }
  }

  def registerEvent = {

  }


}
